int PIN_LEFT_A = 10;
int PIN_LEFT_B = 11;
int PIN_RIGHT_A = 5;
int PIN_RIGHT_B = 6;

int val = 0;  // variable to store the read value

const int FACE_FORWARD = 1;
const int FACE_BACK = 2;

int pwm_vals[4] = { 40, 100, 255 };
int idx = 0;

int direction = FACE_FORWARD;

void setup() {
  Serial.begin(9600);  // open the serial port at 9600 bps:

  pinMode(PIN_LEFT_A, OUTPUT);
  pinMode(PIN_LEFT_B, OUTPUT);

  pinMode(PIN_RIGHT_A, OUTPUT);
  pinMode(PIN_RIGHT_B, OUTPUT);
}

void forward_left(int face) {
  if (FACE_FORWARD == face) {
    analogWrite(PIN_LEFT_A, 100);
    analogWrite(PIN_LEFT_B, 0);
  } else {
    analogWrite(PIN_LEFT_A, 0);
    analogWrite(PIN_LEFT_B, 100);
  }
}
void forward_right(int face) {
  if (FACE_FORWARD == face) {
    analogWrite(PIN_RIGHT_A, 100);
    analogWrite(PIN_RIGHT_B, 0);
  } else {
    analogWrite(PIN_RIGHT_A, 0);
    analogWrite(PIN_RIGHT_B, 100);
  }
}

void stop_left() {
  analogWrite(PIN_LEFT_A, 0);
  analogWrite(PIN_LEFT_B, 0);
}
void stop_right() {
  analogWrite(PIN_RIGHT_A, 0);
  analogWrite(PIN_RIGHT_B, 0);
}

void stop() {
  stop_left();
  stop_right();
}


void move_left(int speed, int face) {
  if (FACE_FORWARD == face) {
    analogWrite(PIN_LEFT_A, speed);
    analogWrite(PIN_LEFT_B, 0);
  } else {
    analogWrite(PIN_LEFT_A, 0);
    analogWrite(PIN_LEFT_B, speed);
  }
}

void move_right(int speed, int face) {
  if (FACE_FORWARD == face) {
    analogWrite(PIN_RIGHT_A, speed);
    analogWrite(PIN_RIGHT_B, 0);
  } else {
    analogWrite(PIN_RIGHT_A, 0);
    analogWrite(PIN_RIGHT_B, speed);
  }
}

const int DELTA = 20;
const int MIN_SPEED = 30;
const int MAX_SPEED = 255;
const int DELAY_MS = 500;


void loop() {
  for (int i = MIN_SPEED; i < 255;) {
    Serial.println(i);
    move_left(i, FACE_FORWARD);
    move_right(i, FACE_FORWARD);
    delay(DELAY_MS);
    i += DELTA;
  }
  stop();
  delay(3000);
  for (int i = MAX_SPEED; i > MIN_SPEED;) {
    Serial.println(i);
    move_left(i, FACE_BACK);
    move_right(i, FACE_BACK);
    delay(DELAY_MS);

    i -= DELTA;
  }
  stop();
  delay(3000);


}